package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.mapper.UserMapper;
import com.fruitshop.model.Command;
import com.fruitshop.model.User;
import com.fruitshop.model.UserInfo;
import com.fruitshop.util.*;

import java.util.ArrayList;
import java.util.Scanner;

public class UserLoginManager extends BaseManager {

    private static final String USER_INFO = "1";
    public UserLoginManager(String title, ArrayList<Command> commandList) {
        // TODO Auto-generated constructor stub
        super(title, commandList);
    }

    @Override
    protected String processCommand(String command) {
        switch (command) {
            case USER_INFO:return showLoginInfo();
        }
        return command;
    }

    private String showLoginInfo() {
        while (true) {
            Scanner sc = new Scanner(System.in);
            String account = ConsoleUtils.readString(sc, "请输入账号:");
            String password = ConsoleUtils.readString(sc, "请输入密码:");


            User user = new User();
            user.setAccount(account);
            user.setPassword(password);
            UserMapper userMapper = DBUtil.getMapper(UserMapper.class);
            User byAccount = userMapper.selectByAccount(user.getAccount());
            if (byAccount == null||!byAccount.getPassword().equals(user.getPassword())) {
                PrintUtils.printError("账号或密码错误");
            } else {
                UserInfoContext.setUserInfo(new UserInfo(null, byAccount));
                PrintUtils.printHighLight("登录成功");
                return LOGIN_SUCCESS;
            }
        }
    }
}
